/**
 * RSI配置参数
 */
let initParam = {
  name: 'RSI',
  type: 'polyline',
  caculateParam: [6, 12, 24]
}

/**
 * Caculator 数据处理
 *
 */
/**
 * Caculator 数据处理
 * @param  {array} rawData
 * @param  {array} caculateParam
 */
function caculate (rawData, caculateParam) {
  if (!(rawData instanceof Array && caculateParam instanceof Array)) throw new Error('Data or caculateParam is not Array')
  let data = rawData.map((d, i) => { return Object.assign({}, d) })
  return RSI(data, caculateParam)
}
function RSI (data, T) {
  let las = [0, 0, 0]
  let lam = [0, 0, 0]
  let firstPoint = {
    time: data[0].time,
    value: 100
  }
  let dataPoints = T.map(d => { return [Object.assign({}, firstPoint)] })
  for (let i = 1; i < data.length; i++) {
    let diff = data[i].close - data[i - 1].close
    let max = Math.max(diff, 0)
    for (let j = 0; j < T.length; j++) {
      let smamax = (max + (T[j] - 1) * las[j]) / T[j]
      let smaAbs = (Math.abs(diff) + (T[j] - 1) * lam[j]) / T[j]
      las[j] = smamax
      lam[j] = smaAbs
      dataPoints[j] = [...dataPoints[j], {
        time: data[i].time,
        value: 100 * smamax / smaAbs
      }]
    }
  }
  return dataPoints
}

export default {
  initParam: initParam,
  caculate: caculate
}
